package com.zerowidth.db.op;

import com.zerowidth.db.DBHelper;
import com.zerowidth.db.base.SqlResult;
import com.zerowidth.db.bean.AuthTokenBean;
import com.zerowidth.db.utils.IOUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

public class AuthTokenDB {

    public static AuthTokenBean findUserByToken(String token) {
        String sql = String.format("SELECT * FROM auth_token where auth_token = '%s'", token);
        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            if (sr == null) {
                return null;
            }

            ResultSet rs = sr.rs;
            if (rs.next()) {
                AuthTokenBean authTokenBean = new AuthTokenBean();
                authTokenBean.id = rs.getInt("id");
                authTokenBean.user_id = rs.getString("user_id");
                authTokenBean.phone_number = rs.getString("phone_number");
                authTokenBean.auth_token = rs.getString("auth_token");
                authTokenBean.create_time = rs.getString("create_time");
                authTokenBean.valid_date = rs.getString("valid_date");

                return authTokenBean;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }

    public static AuthTokenBean findToken(String phoneNumber) {
        String sql = String.format("SELECT * FROM auth_token where phone_number = '%s'", phoneNumber);
        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            if (sr == null) {
                return null;
            }

            ResultSet rs = sr.rs;
            if (rs.next()) {
                AuthTokenBean authTokenBean = new AuthTokenBean();
                authTokenBean.id = rs.getInt("id");
                authTokenBean.user_id = rs.getString("user_id");
                authTokenBean.phone_number = rs.getString("phone_number");
                authTokenBean.auth_token = rs.getString("auth_token");
                authTokenBean.create_time = rs.getString("create_time");
                authTokenBean.valid_date = rs.getString("valid_date");

                return authTokenBean;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }

    public static int clearTokenByPhone(String phoneNumber) {
        String sql = String.format("delete FROM auth_token where phone_number = '%s'", phoneNumber);
        return DBHelper.executeUpdate(sql);
    }

    public static int clearTokenByUserid(String userId) {
        String sql = String.format("delete FROM auth_token where user_id = '%s'", userId);
        return DBHelper.executeUpdate(sql);
    }

    public static int insert(AuthTokenBean tokenBean) {
        if (tokenBean == null) {
            return -1;
        }
        String sql = String.format("INSERT INTO auth_token (phone_number, auth_token, create_time, " +
                        " valid_date, user_id) VALUES ('%s', '%s', '%s', '%s', '%s')",
                tokenBean.phone_number, tokenBean.auth_token, tokenBean.create_time,
                tokenBean.valid_date, tokenBean.user_id);
        return DBHelper.executeUpdate(sql);
    }


}
